package com.loongtech.bi.action.system;

import com.loongtech.bi.entity.system.EntityMonitorConfig;
import com.loongtech.bi.entity.system.EntityMonitorGroupConfig;
import com.loongtech.bi.entity.system.EntityMonitorUserGroup;
import com.loongtech.bi.manager.system.BiMonitorConfigManager;
import com.loongtech.bi.manager.system.BiMonitorGroupConfigManager;
import com.loongtech.bi.manager.system.BiMonitorUserGroupManager;
import com.loongtech.core.util.Utils;
import org.quartz.Trigger.TriggerState;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import java.util.ArrayList;
import java.util.List;

@Controller
@Scope("view")
public class BiMonitorGroupConfigAction {
	@Resource
	BiMonitorGroupConfigManager biMonitorGroupConfigManager;
	@Resource
	BiMonitorConfigManager biMonitorConfigManager;
	@Resource
	BiMonitorUserGroupManager biMonitorUserGroupManager;

	private List<String> typeList;
	private EntityMonitorGroupConfig entityMonitorGroupConfig;
	private List<EntityMonitorGroupConfig> groupList;

	@Autowired
	private void inital() {
		typeList = new ArrayList<>();
		typeList.add("微信");
		typeList.add("手机号");
		typeList.add("语音号");
		typeList.add("邮箱");

		groupList = biMonitorGroupConfigManager.queryList();
		entityMonitorGroupConfig = new EntityMonitorGroupConfig();
	}

	public void searchByCriteria() {
		groupList = biMonitorGroupConfigManager.searchByCriteria(entityMonitorGroupConfig);
	}

	/**
	 * 增加
	 */
	public void addGroup() {
		FacesContext faceContext = FacesContext.getCurrentInstance();
		if (validate(faceContext, entityMonitorGroupConfig)) {
			biMonitorGroupConfigManager.addGroup(entityMonitorGroupConfig);
			faceContext.addMessage(null, new FacesMessage("提示", "添加成功！"));
			inital();
		}
	}

	/**
	 * 删除
	 */
	public void delGroup(String id) {
		FacesContext faceContext = FacesContext.getCurrentInstance();
		
		//分组属于某个任务时，根据任务状态判断是否能删除
		EntityMonitorConfig res = biMonitorConfigManager.getByQuery("from EntityMonitorConfig where groupId = ? and (status = ? or status = ? or status = ? or status = ?)", Integer.parseInt(id), TriggerState.BLOCKED.name(), TriggerState.NORMAL.name(), TriggerState.PAUSED.name(), TriggerState.ERROR.name());
		if (res == null) {
			List<EntityMonitorUserGroup> userGroups = biMonitorUserGroupManager.findByQuery("from EntityMonitorUserGroup where groupId = ?", Integer.parseInt(id));
			if (userGroups != null && !userGroups.isEmpty()) {
				for (EntityMonitorUserGroup userGroup : userGroups) {
					biMonitorUserGroupManager.delUserGroup(userGroup.getId());
				}
			}
			biMonitorGroupConfigManager.delGroup(Integer.parseInt(id));
			faceContext.addMessage(null, new FacesMessage("提示", "删除成功！"));
		} else {
			faceContext.addMessage(null, new FacesMessage("删除失败", "分组属于监控:" + res.getDescription()));
		}
		inital();
	}

	/**
	 * 修改
	 */
	public void modifiyGroup(EntityMonitorGroupConfig e) {
		FacesContext faceContext = FacesContext.getCurrentInstance();
		if (validate(faceContext, e)) {
			biMonitorGroupConfigManager.modifiyGroup(e);
			faceContext.addMessage(null, new FacesMessage("提示", "修改成功！"));
			inital();
		}
	}

	private boolean validate(FacesContext faceContext, EntityMonitorGroupConfig entity) {
		if (Utils.isEmptyStr(entity.getGroupName())) {
			faceContext.addMessage(null, new FacesMessage("提示", "分组名称不能为空！"));
			return false;
		}
        if (Utils.isEmptyStr(entity.getNotifyType())) {
            faceContext.addMessage(null, new FacesMessage("提示", "通知方式不能为空！"));
            return false;
        }
		if (Utils.isEmptyStr(entity.getVoiceTemplate()) && containCheck("语音号", entity.getNotifyTypeList())) {
			faceContext.addMessage(null, new FacesMessage("提示", "语音号已勾选，不能为空！"));
			return false;
		}
		return true;
	}

	private boolean containCheck(String str, List<String> list) {
		if (list == null || list.size() == 0) {
			return false;
		}
		for (String strInList : list) {
			if (strInList.equals(str)) {
				return true;
			}
		}
		return false;
	}

	public EntityMonitorGroupConfig getEntityMonitorGroupConfig() {
		return entityMonitorGroupConfig;
	}

	public void setEntityMonitorGroupConfig(EntityMonitorGroupConfig entityMonitorGroupConfig) {
		this.entityMonitorGroupConfig = entityMonitorGroupConfig;
	}

	public List<EntityMonitorGroupConfig> getGroupList() {
		return groupList;
	}

	public void setGroupList(List<EntityMonitorGroupConfig> groupList) {
		this.groupList = groupList;
	}

	public List<String> getTypeList() {
		return typeList;
	}

	public void setTypeList(List<String> typeList) {
		this.typeList = typeList;
	}
}
